﻿Imports System.IO.TextWriter
Imports System.IO

Public Class PathAndFile

    Shared mProject As String
    Shared mProjectPath As String  '工程路径          \卡号\工程数据\工程名称\
    Shared mSavePath As String     '数据保存路径      \卡号\工程数据\工程名称\水准测量\文件名
    Shared mSvPath As String

    'Shared mSvPath As String = "\SD-MMC card\工程数据\"                        '宏基； 
    'Shared mSvPath As String = "\SD 卡\工程数据\"                             'DELL；
    'Shared mSvPath As String = "\SD Card\工程数据\"                           'HP
    'Shared mSvPath As String = "\Storage Card\工程数据\"                      '手机818,Mio
    'Shared mSvPath As String = "D:\My_VSNet\SD-MMC card\工程数据\"            '桌面版调试

    Sub New(ByVal ProjectName As String)
        SetSvPath()
        mProject = ProjectName
        SetPath()
    End Sub
    Shared Sub SetSvPath()
        Dim sCard As String = String.Empty
        If StorageCard.GetFirstCardName(sCard) Then
            mSvPath = "\" & sCard & "\工程数据\"
        Else
            mSvPath = "\工程数据\"
        End If
    End Sub

    Private Sub SetPath()
        mProjectPath = mSvPath & "" & mProject
        mSavePath = mProjectPath & "\沉降测量\"

        Dim mPath As String = mSvPath & mProject & "\"
        Try
            System.IO.Directory.CreateDirectory(mPath)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    ''' <summary>
    ''' 在“沉降测量”目录中返回已建工程的列表
    ''' 逗号分隔
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property GetCreatedProjects() As String
        Get
            Try
                If mSvPath Is Nothing Then SetSvPath()
                If System.IO.Directory.Exists(mSvPath) Then
                    Dim sF() As String = System.IO.Directory.GetFileSystemEntries(mSvPath)
                    Dim sProjects As String = ""
                    For I As Integer = 0 To sF.Length - 1
                        Dim sFn As String = sF(I)
                        If InStr(sFn, ".") > 0 Then
                        Else
                            Dim pos As Integer = sFn.LastIndexOf("\")
                            sProjects &= Mid(sFn, pos + 2) & ","
                        End If
                    Next
                    Return sProjects
                Else
                    System.IO.Directory.CreateDirectory(mSvPath)
                    Return String.Empty
                End If
            Catch ex As Exception
                'MessageBox.Show(ex.Message)
                Return String.Empty
            End Try
        End Get
    End Property


    ''' <summary>
    ''' 返回目标中已有观测文件
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared ReadOnly Property GetExistFiles() As String
        Get
            Dim Files As String = String.Empty
            Try
                If System.IO.Directory.Exists(mSavePath) Then
                    For Each sF As String In System.IO.Directory.GetFiles(mSavePath)
                        Dim iPos As Integer = sF.LastIndexOf("\")
                        Dim jPos As Integer = sF.LastIndexOf(".")
                        If iPos > 0 Then
                            If jPos > 0 Then
                                sF = Mid(sF, iPos + 2, jPos - iPos - 1)
                            Else
                                sF = Mid(sF, iPos + 2)
                            End If
                            If sF.Length > 4 Then
                                If InStr(sF, "Bak") > 0 Then
                                Else
                                    Files &= sF & ","
                                End If
                            End If
                        End If
                    Next
                End If
                Return Files
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return String.Empty
            End Try
        End Get
    End Property


    '保存数据
    Public Shared Function SaveToFile(ByVal Project As String, ByVal FileName As String, ByVal sData As String) As Boolean
        mProject = Project

        Dim FullFileName As String = mSavePath & FileName & ".Lvl"
        Dim BakFileName = FullFileName.Replace(".Lvl", "Bak.Lvl")

        If ExistFile(FileName) Then
            Try
                If ExistFile(BakFileName) Then System.IO.File.Delete(BakFileName)
            Catch ex As Exception
                MsgBox("删除备份文件出错", ex.Message)
                Return False
            End Try

            Try
                System.IO.File.Copy(FullFileName, BakFileName, True)
            Catch ex As Exception
                MsgBox("保存数据前,文件备份出错", ex.Message)
                Return False
            End Try

            Try
                System.IO.File.Delete(FullFileName)
                Dim FN As System.IO.StreamWriter = New IO.StreamWriter(FullFileName, True)
                FN.WriteLine(sData)
                FN.Close()
                Return True
            Catch ex As Exception
                MsgBox("保存数据出错", ex.Message)
                Return False
            End Try


        Else

            '没找到文件则直接保存
            ExamDictory(FullFileName)
            Dim FN As System.IO.StreamWriter = New IO.StreamWriter(FullFileName, True)
            FN.WriteLine(sData)
            FN.Close()
            Return True

        End If



    End Function

    '保存打印手簿
    Public Shared Function SaveToPrintFile(ByVal Project As String, ByVal FileName As String, ByVal sData As String) As Boolean
        Try
            mProject = Project
            Dim FullFileName As String = mSavePath & FileName & ".prt"
            Dim FN As System.IO.StreamWriter = New IO.StreamWriter(FullFileName, False)
            FN.WriteLine(sData)
            FN.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    '读取数据
    Public Shared Function ReadFile(ByVal project As String, ByVal FileName As String, ByRef sData As String) As Boolean
        If ExistFile(FileName) Then
            Dim FullFileName As String = mSavePath & FileName & ".Lvl"
            Try
                Dim FN As System.IO.TextReader = New IO.StreamReader(FullFileName)
                sData = FN.ReadToEnd
                FN.Close()
                Return True
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
        Return False
    End Function

    '清空数据文件
    Public Shared Function ClearFile(ByVal FileName As String) As Boolean
        Dim FullFileName As String = mSavePath & FileName
        If ExistFile(FullFileName) Then
            Try
                Dim FN As System.IO.TextWriter = New IO.StreamWriter(FullFileName, False)
                FN.WriteLine("")
                FN.Close()
                Return True
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
        Return False
    End Function

    '创建目录
    Public Shared Function ExamDictory(ByVal FullFileName As String) As Boolean
        Dim sPath As String
        Dim sFname As String
        Dim iPos As Integer = FullFileName.LastIndexOf("\")
        If iPos > 0 Then
            sPath = Mid(FullFileName, 1, iPos)
            sFname = Mid(FullFileName, iPos + 2)
            If sPath.Length > 0 AndAlso sFname.Length > 0 Then
                Try
                    System.IO.Directory.CreateDirectory(sPath)
                    Return True
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End If
        Return False
    End Function

    '文件是否存在
    Public Shared Function ExistFile(ByVal FileName As String) As Boolean
        Dim FullFileName As String = mSavePath & FileName & ".Lvl"
        If System.IO.File.Exists(FullFileName) Then
            Return True
        Else
            Return False
        End If

    End Function


    '保存打印手簿
    Public Shared Function RecTime(ByVal sData As String) As Boolean
        Try
            Dim FullFileName As String = "\TimeRec.txt"
            Dim FN As System.IO.StreamWriter = New IO.StreamWriter(FullFileName, True)
            FN.WriteLine(sData)
            FN.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

End Class







Public Class StorageCard
    Public Shared Function GetStorageCardNames(ByRef Cards As String) As Boolean
        Try
            Dim attrStorageCard As FileAttributes = FileAttributes.Directory Or FileAttributes.Temporary
            Dim sCards As String = String.Empty
            Dim rootDir As New DirectoryInfo("\")
            For Each di As DirectoryInfo In rootDir.GetDirectories()
                If (di.Attributes And attrStorageCard) = attrStorageCard Then
                    If sCards = String.Empty Then
                        '第一张卡
                        sCards = di.Name
                    Else
                        '后续卡
                        sCards = sCards + "|" + di.Name
                    End If
                End If
            Next
            Cards = sCards
            Return True
        Catch ex As Exception
            Cards = String.Empty
            Return False
        End Try



    End Function

    Public Shared Function GetFirstCardName(ByRef CardName As String) As Boolean
        Try
            Dim attrStorageCard As FileAttributes = FileAttributes.Directory Or FileAttributes.Temporary
            Dim sCards As String = String.Empty
            Dim rootDir As New DirectoryInfo("\")
            For Each di As DirectoryInfo In rootDir.GetDirectories()
                If (di.Attributes And attrStorageCard) = attrStorageCard Then
                    CardName = di.Name
                    Return True
                End If
            Next
        Catch ex As Exception
            Return False
        End Try
    End Function


End Class




